const reverse = require("./template.js")

function testReverse() {
  console.log(
    "1. 基本字符串反转:",
    (() => {
      const str = "hello"
      const result = reverse(str)
      return result === "olleh"
    })()
  )

  console.log(
    "2. 空字符串:",
    (() => {
      const str = ""
      const result = reverse(str)
      return result === ""
    })()
  )

  console.log(
    "3. 单个字符:",
    (() => {
      const str = "a"
      const result = reverse(str)
      return result === "a"
    })()
  )

  console.log(
    "4. 数字字符串:",
    (() => {
      const str = "12345"
      const result = reverse(str)
      return result === "54321"
    })()
  )

  console.log(
    "5. 包含空格的字符串:",
    (() => {
      const str = "hello world"
      const result = reverse(str)
      return result === "dlrow olleh"
    })()
  )

  console.log(
    "6. 回文字符串:",
    (() => {
      const str = "level"
      const result = reverse(str)
      return result === "level"
    })()
  )

  console.log(
    "7. 特殊字符:",
    (() => {
      const str = "!@#$%^&*()"
      const result = reverse(str)
      return result === ")(*&^%$#@!"
    })()
  )

  console.log(
    "8. 中文字符串:",
    (() => {
      const str = "你好世界"
      const result = reverse(str)
      return result === "界世好你"
    })()
  )

  console.log(
    "9. 混合字符类型:",
    (() => {
      const str = "Hello123!世界"
      const result = reverse(str)
      return result === "界世!321olleH"
    })()
  )

  console.log(
    "10. 长字符串:",
    (() => {
      const str = "abcdefghijklmnopqrstuvwxyz"
      const result = reverse(str)
      return result === "zyxwvutsrqponmlkjihgfedcba"
    })()
  )

  console.log(
    "11. 包含换行符:",
    (() => {
      const str = "line1\nline2"
      const result = reverse(str)
      return result === "2enil\n1enil"
    })()
  )

  console.log(
    "12. 包含制表符:",
    (() => {
      const str = "a\tb\tc"
      const result = reverse(str)
      return result === "c\tb\ta"
    })()
  )

  console.log(
    "13. 重复字符:",
    (() => {
      const str = "aaaaaa"
      const result = reverse(str)
      return result === "aaaaaa"
    })()
  )

  console.log(
    "14. 大小写混合:",
    (() => {
      const str = "AbCdEf"
      const result = reverse(str)
      return result === "fEdCbA"
    })()
  )

  console.log(
    "15. 转义字符:",
    (() => {
      const str = "a\\b\"c'd"
      const result = reverse(str)
      return result === "d'c\"b\\a"
    })()
  )

  console.log(
    "16. 原字符串不变:",
    (() => {
      const str = "original"
      const originalStr = str
      reverse(str)
      return str === originalStr
    })()
  )

  console.log(
    "17. 返回类型检查:",
    (() => {
      const str = "test"
      const result = reverse(str)
      return typeof result === "string"
    })()
  )

  console.log(
    "18. 很长的字符串性能测试:",
    (() => {
      const str = "a".repeat(10000)
      const result = reverse(str)
      return (
        result.length === 10000 && result[0] === "a" && result[9999] === "a"
      )
    })()
  )

  console.log(
    "19. 边界情况 - 两个字符:",
    (() => {
      const str = "ab"
      const result = reverse(str)
      return result === "ba"
    })()
  )

  console.log(
    "20. JSON 字符串:",
    (() => {
      const str = '{"key":"value"}'
      const result = reverse(str)
      return result === '}"eulav":"yek"{'
    })()
  )

  console.log(
    "21. HTML 标签:",
    (() => {
      const str = "<div>Hello</div>"
      const result = reverse(str)
      return result === ">vid/<olleH>vid<"
    })()
  )

  console.log(
    "22. 连续空格:",
    (() => {
      const str = "a   b"
      const result = reverse(str)
      return result === "b   a"
    })()
  )

  console.log(
    "23. 对称性测试:",
    (() => {
      const str = "symmetric"
      const result = reverse(str)
      const doubleReverse = reverse(result)
      return doubleReverse === str
    })()
  )

  console.log(
    "24. 非 ASCII 字符:",
    (() => {
      const str = "café"
      const result = reverse(str)
      return result === "éfac"
    })()
  )

  console.log(
    "25. 多字节Unicode字符(表情符号):",
    (() => {
      const str = "😀😁😂" // 三个表情符号，每个占用4个字节 比如 abcd efgh ffff, 要实现 ffff efgh abcd
      // 倒循环会变成 ffff hgfe dcba, 就是它连字节也反转了，不对。
      const result = reverse(str)
      // 当前实现会错误地反转，导致表情符号乱码
      // 正确的反转应该是保持表情符号的完整性
      const expected = "😂😁😀" // 正确的反转结果
      return result === expected // 这个测试会失败，证明当前实现有问题
    })()
  )

  console.log(
    "26. this上下文测试:",
    (() => {
      const obj = {
        text: "hello",
        reverse: function() {
          return reverse(this.text)
        }
      }
      const result = obj.reverse()
      return result === "olleh"
    })()
  )

  console.log(
    "27. 方法调用this绑定:",
    (() => {
      const obj = {
        prefix: "reversed_",
        process: function(str) {
          const reversed = reverse(str)
          return this.prefix + reversed
        }
      }
      const result = obj.process("test")
      return result === "reversed_tset"
    })()
  )

  console.log(
    "28. 复杂对象this绑定:",
    (() => {
      const obj = {
        data: {
          message: "hello world",
          getReversed: function() {
            return reverse(this.message)
          }
        }
      }
      const result = obj.data.getReversed()
      return result === "dlrow olleh"
    })()
  )

  console.log(
    "29. 回调函数this绑定:",
    (() => {
      const obj = {
        suffix: "_rev",
        process: function(str) {
          return reverse(str) + this.suffix
        }
      }
      const result = obj.process("abc")
      return result === "cba_rev"
    })()
  )
}

testReverse()
